package com.ricervcvcer.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ricervcvcer.common.util.ResultEntity;
import com.ricervcvcer.user.entity.SysPermis;
import com.ricervcvcer.user.service.ISysGrpPerService;
import com.ricervcvcer.user.service.ISysPermisService;
import com.ricervcvcer.user.util.RicervcvcerConstant;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author moge
 * @since 2022-01-10
 */
@RestController
@RequestMapping("sys-permis")
public class SysPermisController {
    @Autowired
    ISysPermisService permisService;

    @Autowired
    ISysGrpPerService grpPerService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @GetMapping
    @ApiOperation("查询所有按钮权限")
    public ResultEntity all(){
        List<SysPermis> list = permisService.list();
        return ResultEntity.successWithData(list);
    }

    @ApiOperation("分页查询权限")
    @PostMapping("page")
    public ResultEntity page(
            @ApiParam("页码") @RequestParam Integer page,
            @ApiParam("每页条数") @RequestParam Integer rows,
            @ApiParam("用户名") @RequestParam String permisName){
        Page<SysPermis> list = permisService.page(page,rows,permisName);
        return ResultEntity.successWithData(list);
    }

    @ApiOperation("根据id修改权限访问信息")
    @PostMapping("update")
    public ResultEntity update(
            @ApiParam("权限信息") @RequestBody SysPermis permis){
        boolean b = permisService.updateById(permis);
        if(!b)
            throw new RuntimeException(RicervcvcerConstant.UPDATE_ERROR);
        refreshRedis();
        return ResultEntity.successWithoutData();
    }

    @ApiOperation("根据id查询权限数据")
    @GetMapping("selectById/{id}")
    public ResultEntity selectById(@ApiParam("权限id") @PathVariable Integer id){
        return ResultEntity.successWithData(permisService.getById(id));
    }

    @ApiOperation("添加一个权限")
    @PostMapping("add")
    public ResultEntity add(
            @ApiParam("权限信息") @RequestBody SysPermis permis){

        // 查询要添加的用户是否已经存在
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("per_url",permis.getPerUrl());
        SysPermis one = permisService.getOne(qw);
        if(!StringUtils.isEmpty(one)){
            throw new RuntimeException("已经存在该权限！");
        }
        boolean b = permisService.save(permis);
        if(!b)
            throw new RuntimeException(RicervcvcerConstant.ADD_ERROR);
        refreshRedis();
        return ResultEntity.successWithoutData();
    }


    @DeleteMapping("delete/{id}")
    @ApiOperation("删除一个权限")
    public ResultEntity delete(@ApiParam("grpId") @PathVariable Integer id){
        // 删除与组的关联表内容
        Map map=new HashMap();
        map.put("gf_per_id",id);
        List list = grpPerService.listByMap(map);
        boolean a=true;
        if(list.size()!=0){
             a = grpPerService.removeByMap(map);
        }
        boolean b = permisService.removeById(id);
        if(!b && !a){
            throw new RuntimeException("删除失败！");
        }
        refreshRedis();
        return ResultEntity.successWithoutData();
    }

    /**
     * 刷新redis
     */
    public void refreshRedis(){
        // TODO 对权限的修改删除之后，更新redis权限信息
//        stringRedisTemplate.delete(RicervcvcerConstant.REDIS_AUTH_PATH);
//        // 查询所有权限,添加到
//        List<SysPermis> sysPermis = permisService.list();
//        Map map=new HashMap();
//        sysPermis.forEach(item->{
//            map.put(item.getPerUrl(),item.getPerContent());
//        });
//        stringRedisTemplate.opsForHash().putAll(RicervcvcerConstant.REDIS_AUTH_PATH,map);

    }
}
